package cn.com.sbell.vss.util;

import java.io.IOException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.com.sbell.vss.Util;
import cn.com.sbell.vss.VssConstants;
import cn.com.sbell.vss.csg.vsclient.util.CSGConfiguration;

public class AlarmRaiser {
	private final static Log log = LogFactory.getLog(AlarmRaiser.class);

	private HttpClient httpClient;

	private String alarmServerUrl;

	public void send(String sourceType, String sourceDeviceId, String sourceIp, String alarmType,
			String data, long timestamp, boolean eliminated) {
		send(sourceType, sourceDeviceId, sourceIp, alarmType, data, timestamp, eliminated,
				null, null);
	}

	public void send(String sourceType, String sourceDeviceId, String sourceIp, String alarmType,
			String data, long timestamp, boolean eliminated,
			String organizationId, String organizationName) {

		PostMethod method = null;
		//add vondart
//		if(CSGConfiguration.pattern.matcher(CSGConfiguration.AS_ALARM_URI).matches())
//		{
//		alarmServerUrl=CSGConfiguration.AS_ALARM_URI;
//		log.debug("changed asUrl in alarm register to: "+alarmServerUrl);
//		}
		if (alarmServerUrl.endsWith("/")) {
			method = new PostMethod(alarmServerUrl + "RaiseAlarm");
		} else {
			method = new PostMethod(alarmServerUrl + "/RaiseAlarm");
		}
		method.getParams().setContentCharset(VssConstants.DEFAULT_ENCODINIG);
		// method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
		// new DefaultHttpMethodRetryHandler(3, false));

		// set param
		method.addParameter("alarmType", alarmType);
		method.addParameter("serverType", sourceType);
		method.addParameter("eliminated", String.valueOf(eliminated));
		if (!Util.strIsNull(sourceDeviceId)) {
			method.addParameter("deviceId", sourceDeviceId);
		}
		if (!Util.strIsNull(sourceIp)) {
			method.addParameter("serverIp", sourceIp);
		}
		if (!Util.strIsNull(data)) {
			method.addParameter("data", data);
		}
		if (!Util.strIsNull(organizationId)) {
			method.addParameter("organizationId", organizationId);
		}
		if (!Util.strIsNull(organizationName)) {
			method.addParameter("organizationName", organizationName);
		}
		method.addParameter("timestamp", String.valueOf(timestamp / 1000L));

		if (log.isDebugEnabled()) {
			log.debug("Sending an alarm to Alarm Server: " + alarmServerUrl);
		}

		try {
			// Execute the method.
			int statusCode = httpClient.executeMethod(method);
			method.getResponseBody();
			if (statusCode != HttpStatus.SC_OK) {
				log.warn("Method failed: " + method.getStatusLine());
			}
		} catch (HttpException ex) {
			log.warn("HTTP protocol violation: ", ex);
		} catch (IOException ex) {
			log.warn("Http transport error: ", ex);
		} catch (Exception ex) {
			log.warn("MS server error", ex);
		} finally {
			// Release the connection.
			method.releaseConnection();
			log.debug("Done.");
		}
	}

	public void setHttpClient(HttpClient httpClient) {
		this.httpClient = httpClient;
	}

	public void setAlarmServerUrl(String alarmServerUrl) {
		this.alarmServerUrl = alarmServerUrl;
	}
}
